﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using QDP.Contracts;

namespace WebSite.Request
{
    public class YunWeiService
    {
        readonly string TOOLBAR_FLAG = "1";

        public void RefreshBuffer()
        {
            ConfigDataBuffer.RefreshBuffer();
        }
        bool isAdmin = false;
        // 获取专题应用权限清单
        List<RelationRoleApp> AuthList = new List<RelationRoleApp>();
        List<string> roleIDs = new List<string>();

        public ConfigObject get_qdp_config(string UserId)
        {
            try
            {
                ConfigDataBuffer.RefreshBuffer();
                #region Initialize
                //构造一个QDP平台参数结构对象的实例。
                ConfigObject CO = new ConfigObject()
                {
                    Success = true
                };
                //初始化数据服务资源参数结构情况。
                CO.GisServiceConfig = new GisServiceConfig();
                // 初始化专题应用资源参数结构情况。
                CO.NavigationConfig = new NavigationConfig();
                // 判断是否拥有权限
                isAdmin = true;//CheckUserIsAdmin(UserId);

                roleIDs = (from relation in ConfigDataBuffer.RelationUserRoles
                           where relation.AR0_UID == UserId
                           select relation.AR1_RID).ToList();

                if (isAdmin == false)
                {
                    // 获取专题应用权限清单
                    AuthList = (from roleID in roleIDs
                                join role_app in ConfigDataBuffer.RelationRoleApps on roleID equals role_app.RoleID
                                select role_app).Distinct().ToList();
                }

                #endregion
                #region 处理应用
                ConfigDataBuffer.Apps.OrderBy(p => p.Order).ToList().ForEach(app =>
                {
                    if (isAdmin == false && AuthList.Any(p => p.AppID.Equals(app.ObjectID)) == false) return; //无应用访问权限
                    NavigationBase TNavDir, MNavDir;
                    var tags = app.Tags.Split('\\');
                    if (tags.Length < 2) return;
                    string topicName = tags[0];
                    string moduleName = tags[tags.Length - 1];
                    var istool = app.Type != TOOLBAR_FLAG; // 判断组件是否为小工具类型
                    if (istool && CO.NavigationConfig.Parts.Any(p => p.Caption.Equals(topicName)))
                    {
                        TNavDir = CO.NavigationConfig.Parts.First(p => p.Caption.Equals(topicName));// as NavDirectory;
                    }
                    else if (istool == false && CO.NavigationConfig.Tools.Any(p => p.Caption.Equals(topicName)))
                    {
                        TNavDir = CO.NavigationConfig.Tools.First(p => p.Caption.Equals(topicName));// as NavDirectory;
                    }
                    else
                    {
                        TNavDir = new NavDirectory()
                        {
                            Caption = topicName,
                            Code = Guid.NewGuid().ToString(),
                            ParmsExpression = ConfigDataBuffer.GetParameter(topicName)
                        };
                        if (istool)
                        {
                            CO.NavigationConfig.Parts.Add(TNavDir);
                        }
                        else
                        {
                            CO.NavigationConfig.Tools.Add(TNavDir);
                        }
                    }

                    if (TNavDir.SubNotes.Any(p => p.Caption.Equals(moduleName)))
                    {
                        MNavDir = TNavDir.SubNotes.First(p => p.Caption.Equals(moduleName));
                    }
                    else
                    {
                        MNavDir = new NavDirectory()
                        {
                            Caption = moduleName,
                            Code = Guid.NewGuid().ToString(),
                            ParmsExpression = ConfigDataBuffer.GetParameter(moduleName)
                        };
                        TNavDir.SubNotes.Add(MNavDir);
                    }
                    var ANavUis = new NavUISLXap()
                    {
                        Caption = app.Caption,
                        Background = app.Bgc,
                        Code = app.Code,
                        Description = app.Descript,
                        Foreground = app.Frc,
                        ImageUrl1 = app.Icon1,
                        ImageUrl2 = app.Icon2,
                        Package = app.URL,
                        ParmsExpression = app.Args,
                        Source = app.Arg1,
                        Type = app.Arg2,
                        Tags = app.Tags
                    };
                    MNavDir.SubNotes.Add(ANavUis);
                });
                #endregion //#region 处理应用
                #region 处理服务
                ConfigDataBuffer.Servicess.OrderBy(p => p.Order).ToList().ForEach(ser =>
                {
                    if (isAdmin == false && AuthList.Any(p => p.AppID.Equals(ser.ObjectID)) == false) return; //无应用访问权限

                    var tags = ser.Tags.Split('\\');
                    if (tags.Length < 2) return;
                    string topicName = tags[0];
                    string moduleName = tags[tags.Length - 1];
                    MapServiceLayerBase TNavDir, MNavDir;
                    if (CO.NavigationConfig.Parts.Any(p => p.Caption.Equals(topicName)))
                    {
                        TNavDir = CO.GisServiceConfig.AutoloadMapServices.First(p => p.Caption.Equals(topicName));// as ServiceDirectory;
                    }
                    else
                    {
                        TNavDir = new MapServiceLayerBase()
                        {
                            Caption = topicName
                        };
                        CO.GisServiceConfig.AutoloadMapServices.Add(TNavDir);
                    }

                    if (TNavDir.SubNotes.Any(p => p.Caption.Equals(moduleName)))
                    {
                        MNavDir = TNavDir.SubNotes.First(p => p.Caption.Equals(moduleName));// as ServiceDirectory;
                    }
                    else
                    {
                        MNavDir = new MapServiceLayerBase()
                        {
                            Caption = moduleName
                        };
                        TNavDir.SubNotes.Add(MNavDir);
                    }
                    MapServiceLayerBase Layer = new MapServiceLayerBase();
                    Layer.Type = ser.Type;
                    Layer.Code = ser.Code;
                    Layer.Caption = ser.Caption;
                    Layer.Url = ser.URL;
                    Layer.Token = ser.Token;
                    Layer.Description = ser.Descript;
                    Layer.ImageUrl1 = ser.Icon1;
                    Layer.Tags = ser.Tags;
                    Layer.ParmsExpression = ser.Args;
                    MNavDir.SubNotes.Add(Layer);
                });
                #endregion //#region 处理服务
                return CO;
            }
            catch (Exception ex)
            {
                return new ConfigObject()
                {
                    Success = false,
                    Message = ex.ToString()
                };
            }
        }
    }
}